﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Interfaces;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;


namespace Wise.TimeWarrior.Impl
{
    public class ExcelExportData : IExprotData
    {
        public void ExportWorkingDayData(List<WorkingDayDto> DaysToExport)
        {
            if (DaysToExport == null)
            {
                MessageBox.Show("Please select Date Range");
                return;
            }
            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("WorkLog");
            
            // Add header labels
            var rowIndex = 0;
            var row = sheet.CreateRow(rowIndex);
            row.CreateCell(0).SetCellValue("Entry Creation Date");
            row.CreateCell(1).SetCellValue("Day name");
            row.CreateCell(2).SetCellValue("First Event Date");
            row.CreateCell(3).SetCellValue("First Event Name");
            row.CreateCell(4).SetCellValue("Last Event Date");
            row.CreateCell(5).SetCellValue("Last Event Name");
            row.CreateCell(6).SetCellValue("Working Time");
            rowIndex++;

            
            // Add data rows
            foreach (WorkingDayDto workingDayDto in DaysToExport)
            {
                row = sheet.CreateRow(rowIndex);
                row.CreateCell(0).SetCellValue(workingDayDto.CreationDateTime.ToString());
                row.CreateCell(1).SetCellValue(workingDayDto.CreationDateTime.ToString("ddd"));
                row.CreateCell(2).SetCellValue(workingDayDto.FirstAction.EventDate.ToString());
                row.CreateCell(3).SetCellValue(workingDayDto.FirstAction.UserAction.ToString());
                row.CreateCell(4).SetCellValue(workingDayDto.LastAction.EventDate.ToString());
                row.CreateCell(5).SetCellValue(workingDayDto.LastAction.UserAction.ToString());
                row.CreateCell(6).SetCellValue((workingDayDto.LastAction.EventDate.Subtract(workingDayDto.FirstAction.EventDate).ToString()));
                rowIndex++;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.SupportMultiDottedExtensions = false;
            sfd.Filter = "Excel Document (*.xls)|*.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                // Save the Excel spreadsheet to a file on the web server's file system
                using (var fileData = new FileStream(sfd.FileName, FileMode.Create))
                {
                    workbook.Write(fileData);
                }

                Process p = new Process();
                p.StartInfo.FileName = sfd.FileName;
                p.Start();
            }

        }

        public void ExportUserEventsData(List<UserEventDto> EventsToExport)
        {
            throw new NotImplementedException();
        }
    }
}
